import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    upload_status: 0, //0预览 1是审核失败
    name: "",
    legal: "",
    region: "",
    contact_name: "",
    contact_mobile: "",
    product_name: "",
    product_cover: [],
    product_description: [],
    logo: [],
    company_introduction: [],
    //失败的时候用到
    reason:"",
    id:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    const upload_status = options.upload_status;
    that.setData({
      upload_status: options.upload_status
    })
    if (upload_status == 1) {
      this.getNet();
    } else {
      this.getStorage();
    }
  },
  getNet() {
    var that = this;
    wx.request({
      url: 'https://api.dianshiyexiao.com/weapp/company/info',
      method: 'POST',
      header: {
        'Authorization': wx.getStorageSync('token') // 默认值
      },
      success(res) {
        console.log(res.data)
        if (res.data.data.info) {
          that.setData({
            name: res.data.data.info.name,
            legal: res.data.data.info.legal,
            region: res.data.data.info.region,
            contact_name: res.data.data.info.contact_name,
            contact_mobile: res.data.data.info.contact_mobile,
            product_name: res.data.data.info.product_name,
            product_cover: that.stringToImgList(res.data.data.info.product_cover),
            product_description: that.stringToImgList(res.data.data.info.product_description),
            logo: that.stringToImgList(res.data.data.info.logo),
            company_introduction: that.stringToImgList(res.data.data.info.company_introduction),
            //失败的时候会用到
            reason:res.data.data.info.reason,
            id:res.data.data.info.id
          })
        } else {
          that.setData({
            status: -1
          })
        }
      },
      fail(error) {
        console.log(error)
      },
    })
  },
  getStorage() {
    var that = this;
    wx.getStorage({
      key: 'yulan',
      success(res) {
        console.log(res.data)
        that.setData(res.data)
      }
    })
  },
  deleteImg(event) {
    var that = this;
    var xxxid = event.currentTarget.id;
    console.log(xxxid)
    var list = that.data[xxxid]
    console.log(list)
    list.splice(event.detail.index, 1);
    console.log(list)
    this.setData({
      [xxxid]: list
    })
  },
  afterRead(event) {
    var that = this;
    var xxxid = event.currentTarget.id;
    console.log(event)
    if ([xxxid] == "product_cover" || [xxxid] == "logo") {
      wx.cropImage({
        src: event.detail.file.tempFilePath, // 图片路径
        cropScale: '1:1', // 裁剪比例
        success: (res) => {
          console.log(res.tempFilePath)
          var n = {
            "filePath": res.tempFilePath,
            "key": xxxid
          }
          that.upload(n)
        }
      })
    } else {
      var n = {
        "filePath": event.detail.file.tempFilePath,
        "key": xxxid
      }
      that.upload(n)
    }

  },
  upload(n) {
    var that = this;
    console.log(n)
    wx.showLoading({
      title: '上传图片中',
    })
    wx.uploadFile({
      url: 'https://api.dianshiyexiao.com/upload',
      //url:'https://api.xuexi.haizb.cn/upload',
      filePath: n.filePath,
      name: 'file',
      success(res) {
        console.log(res)
        if (res.statusCode != 200) {
          //Toast.fail("上传失败" + res.statusCode)
          Toast({
            duration: 3000,
            message:res.statusCode
          })
          return
        }
        var data = JSON.parse(res.data);
        if (data.status != 200) {
          //Toast.fail("上传失败" + data.status)
          Toast({
            duration: 3000,
            message:data.status
          })
        }
        const img_url = data.data.url;
        console.log("上传回来的" + img_url)
        var list = that.data[n.key]
        list.push({
          "url": img_url
        })
        that.setData({
          [n.key]: list
        })
        Toast.success("上传成功")
      },
      fail(error) {
        console.log(error)
        Toast({
          duration: 3000,
          message:error.errMsg
        })
      },
      complete() {
        //Toast("上传完成")
        wx.hideLoading();
      }
    })
  },
  yulan(){
    wx.setStorage({
      key:"yulan",
      data:this.data,
      success(){
        wx.navigateTo({
          url: '/pages/yulan/yulan?upload_status=0',
        }) 
      }
    })
  },
  stringToImgList(str) {
    var list = [];
    var array = str.split(",")
    array.forEach(item => {
      list.push({
        "url": item
      })
    });
    return list;
  }
})